package hc.bre.rest.cmn

import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import javax.annotation.PostConstruct
import org.apache.catalina.connector.Connector
import org.apache.tomcat.util.descriptor.web.SecurityCollection
import org.apache.tomcat.util.descriptor.web.SecurityConstraint
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.web.servlet.server.ServletWebServerFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered
import org.springframework.stereotype.Service


@Configuration
@groovy.transform.CompileStatic
class WebConfiguration implements WebMvcConfigurer{
	private static final Logger _log = LoggerFactory.getLogger(WebConfiguration);

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		//默认地址（可以是页面或后台请求接口）
		registry.addViewController("/").setViewName("forward:/index.htm");
		//设置过滤优先级最高
		registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
	}

	@Bean
	@ConditionalOnProperty(name = "rule.server.http", havingValue = "on", matchIfMissing = false)
	public ServletWebServerFactory servletContainer(@Value('${rule.server.http.port}')Integer httpport) {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();

		Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
		connector.setPort(httpport);

		tomcat.addAdditionalTomcatConnectors(connector);

		_log.info("http is supported on ${httpport},which can be closed by set property [server.http=off]")
		return tomcat;
	}
}
